package com.freedom.start.common;

import com.freedom.start.sys.entity.UserEntity;
import com.freedom.start.sys.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.Collection;

import static org.apache.shiro.web.filter.authc.FormAuthenticationFilter.*;

/**
 * @Author: zhenggang.liu
 * @Date: 2019/8/9 9:33
 */
@Controller
public class CommonController {

    @Autowired
    private CommonService service;
    @Autowired
    private UserService userService;

    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }

    @RequiresRoles("admin")
    @RequestMapping("/admin")
    public String admin() {
        return "admin";
    }

    @RequiresRoles("user")
    @RequestMapping("/user")
    public String user() {
        return "user";
    }

    @RequestMapping("/remember")
    public String remember() {
        return "remember";
    }

    @RequestMapping("/index")
    public String index(Model model,HttpServletRequest request) {
        Object principal = SecurityUtils.getSubject().getPrincipal();
        String loginName = (String) principal;
        UserEntity user = userService.getByUsername(loginName);
        // userService
        model.addAttribute("user", user);

        return "index";
    }

    @RequestMapping(value = {"/login", "/"}, method = RequestMethod.GET)
    public String login(Model model, HttpServletRequest request) {
        Object principal = SecurityUtils.getSubject().getPrincipal();
        boolean authenticated = SecurityUtils.getSubject().isAuthenticated();

        /*
        此处读取cas认证时认证错误，因为是重定向而来，request中不能读取到，需要放在session中，读取到后立即清除
         */
        String message = (String) request.getSession().getAttribute("message");
        request.getSession().removeAttribute("message");
        model.addAttribute("message", message);

        if (authenticated) {
            // if (principal != null) {
            return "redirect:/index";
        }
        return "login";
    }

    @RequestMapping("/haha")
    @ResponseBody
    public String outString() {
        return "hahaha";
    }

    @RequestMapping("/unauthorize")
    public String error() {
        return "unauthorize";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String doLogin(HttpServletRequest request, Model model) {
        String clazz = Thread.currentThread().getStackTrace()[1].getClassName();
        String method = Thread.currentThread().getStackTrace()[1].getMethodName();
        System.out.println("class name: " + clazz + ", Method Name: " + method);
        Object principal = SecurityUtils.getSubject().getPrincipal();
        boolean authenticated = SecurityUtils.getSubject().isAuthenticated();
        if (authenticated) {
            // if (principal != null) {
            return "redirect:/index";
        }
        String username = WebUtils.getCleanParam(request, DEFAULT_USERNAME_PARAM);
        String password = WebUtils.getCleanParam(request, DEFAULT_PASSWORD_PARAM);
        boolean rememberMe = WebUtils.isTrue(request, DEFAULT_REMEMBER_ME_PARAM);


        String message = (String) request.getAttribute("message");

        model.addAttribute("message", message);
        model.addAttribute("username", username);
        model.addAttribute("password", password);
        model.addAttribute("rememberMe", rememberMe);
        return "login";
    }

    @RequestMapping("/listSession")
    @ResponseBody
    public Collection listActiveSession() throws JSONException {

             return service.listActiveSession();

    }

}
